#include "book_change.h"
#include "ui_book_change.h"

book_change::book_change(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::book_change)
{
    ui->setupUi(this);
    global::bok=new book_sql;
    global::bok->book_db;
    model=new QSqlTableModel(this,global::bok->book_db);//设置表
    model->setTable("book_info");
    model->select();//第一遍筛选
    model->setEditStrategy(QSqlTableModel::OnManualSubmit);//设置表可以编辑
    ui->tableView->setModel(model);//将表绑定到tableview上
    model->setTable("book_info");
    model->select();
    ui->tableView->resizeColumnsToContents();
    ui->tableView->resizeRowsToContents();
    ui->tableView->setAlternatingRowColors(true);
}

book_change::~book_change()
{
    delete ui;
}

void book_change::set_table_model(QStandardItemModel *model)
{ui->tableView->setModel(model);
 ui->tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);


}


void book_change::on_Add_clicked()
{
    while(model->canFetchMore())
        {model->fetchMore();

        }
     int rowCount = model->rowCount();
     model->insertRow(rowCount);
     model->setData(model->index(rowCount,0),"");
     ui->tableView->setEditTriggers(QTableView::DoubleClicked);

     ui->tableView->scrollToBottom();
}


void book_change::on_Delete_clicked()
{
    auto index = ui->tableView->currentIndex();
     model->removeRow(index.row());
     ui->tableView->repaint();

}


void book_change::on_Submit_clicked()
{
    model->submitAll();
    QMessageBox::information(this,tr("提示对话框"),QString("提交成功"),QMessageBox::Ok);
}


void book_change::on_Recall_clicked()
{
    model->revertAll();
     model->submitAll();
}


void book_change::on_Edit_clicked()
{
    if(is_editing==false)
     {ui->Add->setDisabled(true);
     ui->Delete->setDisabled(true);
     ui->Submit->setDisabled(true);
     ui->Edit->setText(QString("完成编辑"));
     is_editing=true;
     ui->tableView->setEditTriggers(QAbstractItemView::DoubleClicked);
        }
     else
     {ui->Add->setDisabled(false);
            ui->Delete->setDisabled(false);
            ui->Submit->setDisabled(false);
            ui->Edit->setText(QString("编辑"));
            is_editing=false;
            ui->tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);
    }
}

